// components/home/index.js
var app = getApp();
import {
    translate
} from '../../api/api.js';

Component({
    properties: {
        animation: {
            type: Boolean,
            value: false,
        },
        refuse: {
            type: Boolean,
            value: false,
        }
    },
    data: { },
    pageLifetimes: {
        // 组件所在页面的生命周期函数
        show: function() {
            this.getLocation();
        },
    },
    methods: {
        getLocation: function(status = false) {
            var that = this
            wx.getLocation({
                type: 'wgs84',
                success: function(res) {
                    if (res.errMsg == 'getLocation:ok') {
                        var timestamp = Date.parse(new Date()) / 1000;
                        var invalidtime = wx.getStorageSync('invalidtime');//获取失效时间
                        //首次授权
                        if(invalidtime == ''){
                           status = true;
                        }
                        if ((invalidtime == '' || invalidtime == null) || timestamp >= invalidtime){
                            wx.request({
                                url: 'https://apis.map.qq.com/ws/geocoder/v1/',
                                header: {
                                    'Content-Type': 'application/x-www-form-urlencoded'
                                },
                                data: {
                                    location: res.latitude + ',' + res.longitude,
                                    key: 'RJMBZ-WC4LU-2MFVT-4AEKB-CJCHZ-6LBDH'
                                },
                                method: 'get',
                                success: function(res) {
                                    if (res.data.message == "query ok") {
                                        that.setData({
                                            refuse: false,
                                            animation: false
                                        })
                                        wx.setStorageSync('invalidtime', timestamp+300);//设置失效时间
                                        wx.setStorageSync('location', res.data.result);//坐标信息  
                                       if (status) {
                                          that.triggerEvent('allowFun')
                                       }
                                    }
                                }
                            })

                        }else{
                           that.setData({
                              refuse: false,
                              animation: false
                           })
                           if (status) {
                              that.triggerEvent('allowFun')
                           }
                        }
                        that.triggerEvent('get_emit')
                    }
                },
                fail: function(res) {
                    var refuse = that.data.refuse
                    var animation = that.data.animation
                    that.setData({
                        refuse: true,
                        animation: false
                    })
                }
            })
        },
        showSettingToast: function() {
            let that = this;
            wx.openSetting({
                success(res) {

                    //如果再次拒绝则返回页面并提示
                    if (!res.authSetting['scope.userLocation']) {
                        wx.showToast({
                            title: '此功能需获取位置信息，请重新设置',
                            duration: 3000,
                            icon: 'none'
                        })
                    } else {
                        that.getLocation(true);
                        
                    }
                }
            })
        }
    }
})